package priv.books.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import priv.books.pojo.Order;
import priv.books.pojo.OrderDetail;
import priv.books.pojo.OrderDetailFrom;
import priv.books.pojo.ResultMessage;
import priv.books.service.OrderDetailService;
import priv.books.service.OrderService;

import java.util.List;

/**
 * 书籍类型控制器
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 修改信息
     * @param order
     * @return
     */
    @PostMapping("/updateInfo")
    public ResultMessage updateInfo(Order order) {
        try {
            // 调用service 修改方法
            boolean result = orderService.updateById(order);
            if(result) {
                return new ResultMessage(0, "操作成功！");
            }
            return new ResultMessage(207, "操作无响应！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(500, "出现异常：" + e.getMessage());
        }
    }

    /**
     * 新增信息
     * @param orderDetailFrom
     * @return
     */
    @PostMapping("/insertInfo")
    public ResultMessage insertInfo(OrderDetailFrom orderDetailFrom) {
        try {
            // 插入订单
            boolean result = orderService.insertInfo(orderDetailFrom);
            if(result) {
                return new ResultMessage(0, "操作成功！");
            }
            return new ResultMessage(207, "操作无响应！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(500, "出现异常：" + e.getMessage());
        }
    }

    /**
     * 删除信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteInfo")
    public ResultMessage deleteInfo(Integer id, String ids) {
        try {
            // 调用service 修改方法
            boolean result = false;
            if(id != null && id != 0) {
                // 删除订单详情
                orderDetailService.remove(new QueryWrapper<OrderDetail>().eq("order_id", id));
                // 删除订单信息
                result = orderService.remove(new QueryWrapper<Order>().eq("id", id));
            } else if(!StrUtil.isBlankOrUndefined(ids)) {
                // 删除订单详情
                orderDetailService.remove(new QueryWrapper<OrderDetail>().in("order_id", StrUtil.split(ids, ',', true, true)));
                // 删除订单信息
                result = orderService.removeByIds(StrUtil.split(ids, ',', true, true));
            }
            if(result) {
                return new ResultMessage(0, "操作成功！");
            }
            return new ResultMessage(207, "操作无响应！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultMessage(500, "出现异常：" + e.getMessage());
        }
    }

}
